package com.kingbi.system.role.service;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.BasicDynaBean;
import org.apache.log4j.Logger;

import com.kingbi.system.function.service.FunctionService;
import com.kingbi.system.roleFunction.service.RoleFunctionService;
import com.kingbi.util.ConstantFiles.SysMagContant;

public class RoleHelper
{
    private static RoleHelper instance;
    
    private static RoleHelper instanceFun;

    RoleService roleService;
    
    FunctionService functionService;
    
    RoleFunctionService roleFunctionService;

    static Logger logger = Logger.getLogger(RoleHelper.class);
    
    public static RoleHelper getInstance(RoleService rService,FunctionService funService,RoleFunctionService rfService)
    {
        if(instance == null)
        {
            instance = new RoleHelper();
            instance.roleService = rService;
            instance.functionService = funService;
            instance.roleFunctionService = rfService;
        }
        return instance;
    }

    /**
     * 构造功能树
     * 
     * @return
     * @throws SQLException 
     */
    public String createFunctionTree() throws SQLException
    {
        String sbToStr = "";
        try
        {
            String queryOrgTreeSql = "select fun.fun_id,fun.func_code,fun.name,fun.func_level,fun.father_fun_code"
                    + " from p_function fun where fun.fun_status = 1";
            List<Object> listOrgTree = functionService.loadOrgTree(queryOrgTreeSql, null);
            BasicDynaBean bean = null;
            Iterator<Object> iterOrgTree = listOrgTree.iterator();
            StringBuffer sb = new StringBuffer();
            sb.append("[");
            while (iterOrgTree.hasNext())
            {
                bean = (BasicDynaBean) iterOrgTree.next();
                sb.append("[");
                sb.append("'" + bean.get("func_code") + "',");
                sb.append("'" + bean.get("name") + "',");
                sb.append("'" + bean.get("father_fun_code")+ "',");
                sb.append(SysMagContant.FUNCTION_MAG);
                sb.append("],");
            }
            sbToStr = sb.toString();
            sbToStr = sbToStr.substring(0, sbToStr.length() - 1);
            sbToStr = sbToStr+ "]";
            logger.warn("**********************setDtree()" + sbToStr + "]");
        }      
        catch (SQLException se)
        {
            throw new SQLException("" + "RoleHelper.createFunctionTree" + se);
        }
        return sbToStr;
    }

}
